<?php
	/////////////////////////////////////////////////////////
	//Description: Task index view
	//Variable: 
	//		- $group_list: list of task group
	// 
	$project_id = Request::current()->param('project_id', NULL);
	$is_one_project = is_null($project_id) ? FALSE : TRUE; 
?>
<?php 
	echo View::factory('project/partials/top_control',array('project' => isset($cur_project) ? $cur_project : NULL ))
?>
<?php 
	//echo view::factory('task/partials/add_task', array('users' => $users, 'project' => $project));
?>
<div class='tstyle_content'>
	<?php echo View::factory('task/partials/add_task_none_list',  array('users' => $users, 'project' => $project))?>
	<?php for ($i =  0; $i<count($tasks) && count($tasks) > 0; $i++):?>
		$t = $tasks[$i];
		<?php if(($i == 0) || ($i>0 && ($t[$i]['list_id'] != $t[$i-1]['list_id']))):?>
		<div class="list">
		<a class="open-closed active" onClick="hide_show_group(event);" href="#"> Open/Closed </a>
		<h8><?php echo $t['list_name']?></h8>
		<div class="list-main-body">
			<table class="list 1 box-skin" style="width: 100%;">
				<tr class="rowA">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>
		<?php endif;?>
			<?php if ($t['status']==Helper_MyConst::STATUS_IN_PROGRESS):?>
				<tr class="rowB">
			<?php elseif ($t['status']==Helper_MyConst::STATUS_COMPLETE):?>
				<tr class="rowC">
			<?php endif;?>
					<td>
						<input type="checkbox" value="<?php echo $t['id']?>" onClick="close_task(event);" class="close-checkbox" > <?php echo $t['name']?>
						<div class="options">
							<a href="#" class="trigger">
								<img class="icon1" src="#">
							</a>
							<div class="sliding">
								<ul>
									<li><a class="edit-task" onClick="edit_task(event);" href="#">Edit</a></li>
									<li><a href="#">Move to</a></li>
									<li><a class="delete-task" href="#">Delete</a></li>
								</ul>
							</div>
						</div>
					</td>
					<td><?php echo $t['assign_user_displayname']?></td>
					<td><?php echo $t['user_displayname']?></td>
					<td><?php echo Helper_MyConst::$PROJECT_PRIORITY_TABLE[$t['priority']]?></td>
					<td><?php echo $t['deadline']?></td>
					<td><?php echo Helper_MyConst::$TASK_STATUS_TABLE[$t['assign_task_status']]?></td>			
				</tr>
		<?php if($t[$i]['list_id'] != $t[$i-1]['list_id']):?>
			</table>
		</div>
		<?php endif;?>
	<?php endfor;?>
</div>
	<?php foreach ($group_list as $g):?>
	<div class="list">
		<a class="open-closed active" onClick="hide_show_group(event);" href="#"> Open/Closed </a>
		<h8><?php echo $g['name']?></h8>
		<div class="list-main-body">
			<table class="list 1 box-skin" style="width: 100%;">
				<tr class="rowA">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>
				<?php foreach ($g['tasks'] as $t):?>
				<tr class="rowB">
					<?php if(!$is_one_project):?><td><a href="/project/<?php echo $t['project_id']?>/setting"><?php echo $t['project_name']?></a></td><?php endif;?>
					<td>
						<input type="checkbox" value="<?php echo $t['id']?>" onClick="close_task(event);" class="close-checkbox" > <?php echo $t['name']?>
						<div class="options">
							<a href="#" class="trigger">
								<img class="icon1" src="#">
							</a>
							<div class="sliding">
								<ul>
									<li><a class="edit-task" onClick="edit_task(event);" href="#">Edit</a></li>
									<li><a href="#">Move to</a></li>
									<li><a class="delete-task" href="#">Delete</a></li>
								</ul>
							</div>
						</div>
					</td>
					<td><?php echo $t['assign_user_displayname']?></td>
					<td><?php echo $t['user_displayname']?></td>
					<td><?php echo Helper_MyConst::$PROJECT_PRIORITY_TABLE[$t['priority']]?></td>
					<td><?php echo $t['deadline']?></td>
					<td><?php echo Helper_MyConst::$TASK_STATUS_TABLE[$t['assign_task_status']]?></td>
				</tr>	
				<?php endforeach;?>
			</table>
		</div>
	</div>
	<?php endforeach;?>	
	<div class="list">
		<a class="open-closed active" onClick="hide_show_group(event);" href="#"> Open/Closed </a>
		<h8> Test list</h8>
		<div class="list-main-body">
			<table class="list 1 box-skin" style="width: 100%;">
				<tr class="rowA">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>
				<?php foreach ($tasks as $task):?>
					<tr class="rowB">
						<?php if(!$is_one_project):?><td><a href="/project/<?php echo $task['project_id']?>/setting"><?php echo $task['project_name']?></a></td><?php endif;?>
						<td>
							<input type="checkbox" value="<?php echo $task['id']?>" onClick="close_task(event);" class="close-checkbox" > <?php echo $task['name']?>
							<div class="options">
								<a href="#" class="trigger">
									<img class="icon1" src="#">
								</a>
								<div class="sliding">
									<ul>
										<li><a class="edit-task" onClick="edit_task(event);" href="#">Edit</a></li>
										<li><a href="#">Move to</a></li>
										<li><a class="delete-task" href="#">Delete</a></li>
									</ul>
								</div>
							</div>
						</td>
						<td><?php echo $task['assign_user_displayname']?></td>
						<td><?php echo $task['user_displayname']?></td>
						<td><?php echo Helper_MyConst::$PROJECT_PRIORITY_TABLE[$task['priority']]?></td>
						<td><?php echo $task['deadline']?></td>
						<td><?php echo Helper_MyConst::$TASK_STATUS_TABLE[$task['assign_task_status']]?></td>
					</tr>
				<?php endforeach;?>
				<?php /*<tr class="row edit-task">
					<td colspan="6">
						<div class="wrap-edit-task">
		 					<label>
		 						<strong>Title :</strong>
		 						<input type="text" name="title" value="">
		 					</label>
		 					<label>
		 						<strong>Description :</strong>
		 						<textarea name="decription"></textarea>
		 					</label>
		 					<label class="quarter">
		 						<strong>Deadline :</strong>
		 						<input type="text" class="deadline small" name="deadline" value="">
		 					</label>
		 					<label class="quarter">
		 						<strong>Assign to :</strong>
		 						<select name="employee">
		 						    
		 							<option>To Anh</option>
		 							<option>Loc Nguyen</option>
		 						</select>
		 					</label>
		 					<label class="quarter">
		 						<strong>Priority :</strong>
		 						<select name="priority">
		 							<option>Low</option>
		 							<option>Medium</option>
		 							<option>High</option>
		 						</select>
		 					</label>
		 					<label class="quarter">
		 						<button name="save" clas="save">Save</button>
		 						<a href="#">Cancel	</a>
		 					</label>
 						</div>
					</td>
				</tr>
				<tr class="rowC">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>*/?>
				
					<?php 
						echo view::factory('task/partials/add_task', array('users' => $users, 'project' => $project, 'list_id' => 1));
					?>
					
				<tr><td colspan="6"><a class="add-new-task" href="#">Add new task</a></td></tr>
			</table>
			
		</div>
	</div>
	<div class="list">
		<a class="open-closed active" onClick="hide_show_group(event);" href="#"> Open/Closed </a>
		<h8> Test list</h8>
		<div class="list-main-body">
			<table class="list 2 box-skin" style="width: 100%;">
				<tr class="rowA">
					<?php if(!$is_one_project):?><td><b>Project name</b></td><?php endif;?>
					<td><b>Tasks</b></td>
					<td><b>Assign to</b></td>
					<td><b>Assigned from</b></td>
					<td><b>Priority</b></td>
					<td><b>Deadline</b></td>
					<td><b>Status</b></td>
				</tr>
				<?php foreach ($tasks as $task):?>
					<tr class="rowB">
						<?php if(!$is_one_project):?><td><a href="/project/<?php echo $task['project_id']?>/setting"><?php echo $task['project_name']?></a></td><?php endif;?>
						<td><input type="checkbox" value="">  <?php echo $task['name']?></td>
						<td><?php echo $task['assign_user_displayname']?></td>
						<td><?php echo $task['user_displayname']?></td>
						<td><?php echo Helper_MyConst::$PROJECT_PRIORITY_TABLE[$task['priority']]?></td>
						<td><?php echo $task['deadline']?></td>
						<td><?php echo Helper_MyConst::$TASK_STATUS_TABLE[$task['assign_task_status']]?></td>
					</tr>
				<?php endforeach;?>
			</table>
		</div>
	</div>
	<?php /*
	<div class='wrap_option' style="width: 14%">
		<div class="option">
		<ul>
			<?php if(!$is_one_project):?><li><?php echo Form::checkbox('Project name', Helper_MyConst::TASK_PROJECT_NAME ,TRUE,array('class'=>'data_option'))?> Project name</li><?php endif;?>
			<li><?php echo Form::checkbox('Tasks', Helper_MyConst::TASK_TASKS,TRUE,array('class'=>'data_option'))?> Tasks</li>
			<li><?php echo Form::checkbox('People', Helper_MyConst::TASK_PEOPLE,TRUE,array('class'=>'data_option'))?> People</li>
			<li><?php echo Form::checkbox('Assigned from', Helper_MyConst::TASK_ASSIGNED_FROM,TRUE,array('class'=>'data_option'))?> Assigned from</li>
			<li><?php echo Form::checkbox('Priotity', Helper_MyConst::TASK_PRIORITY,TRUE,array('class'=>'data_option'))?> Priority</li>
			<li><?php echo Form::checkbox('Deadline', Helper_MyConst::TASK_DEADLINE,TRUE,array('class'=>'data_option'))?> Deadline</li>
			<li><?php echo Form::checkbox('Status', Helper_MyConst::TASK_STATUS,TRUE,array('class'=>'data_option'))?> Status</li>
		</ul>
		</div>
		<div class='direction'><a href="#">>></a></div>
	</div>
	*/	?>
</div>


<script type="text/javascript">
	var task = $.parseJSON('<?php echo addslashes(json_encode($tasks))?>');
	var tasks = [];
	tasks.push(task);
	tasks.push(task);
	console.log(tasks);

	var users = $.parseJSON('<?php echo addslashes(json_encode($users))?>');
	
	var allOptions = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$TASK_OPTION_2_NAME))?>');
	var assign_status = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$TASK_STATUS_TABLE))?>');
	var priority = $.parseJSON('<?php echo addslashes(json_encode(Helper_MyConst::$PROJECT_PRIORITY_TABLE))?>');
	$('input.data_option').change(function(){
		var table = $('div.tstyle_content table');
		var options = [];
		var names = [];
		
		//alert($('input.data_option:checked').length);
		$('input.data_option:checked').each(function() {
			   names.push($(this).attr('name'));
		       options.push($(this).val());
	     });
	    renderTable(options, names, tasks);
	});
	function renderTable(options, names, tasks)
	{
		var oLength = options.length;
		var dLength = tasks.length;
		var table = $('div.tstyle_content table');
		table.empty();
		var htmlStr = '';
		htmlStr+='<tr class="rowA">';
		for (var i = 0; i<oLength; i++)
			htmlStr+='<td><b>'+names[i]+'</b></td>';
		htmlStr+='</tr>';
		
		for (var i = 0; i<dLength; i++)
		{
			htmlStr+='<tr class="rowB">';
			for(var j = 0; j<oLength; j++)
			{
				if (options[j] == <?php echo Helper_MyConst::TASK_PROJECT_NAME?>)
					htmlStr+='<td><a href="/project/'+tasks[i].id+'">'+tasks[i][allOptions[options[j]]]+'</a></td>';
				else if(options[j] == <?php echo Helper_MyConst::TASK_STATUS?>) 
					htmlStr+='<td>'+assign_status[tasks[i][allOptions[options[j]]]]+'</td>';
				else if(options[j] == <?php echo Helper_MyConst::TASK_PRIORITY?>)
					htmlStr+='<td>'+priority[tasks[i][allOptions[options[j]]]]+'</td>'; 
				else
					htmlStr+='<td>'+tasks[i][allOptions[options[j]]]+'</td>';
			}
			htmlStr+='</tr>';
		}
		table.append(htmlStr);
	}

	//add task javascript handle
	function new_task()
	{
		$('div.top_control').hide();
		$('div.new-ele').show();
	}
	$('a#done_add_task').click(function(e){
		e.preventDefault();
		$('div.new-ele').hide();
		$('div.top_control').show();
	});
	function add_new_task(new_task, tr, newList, targetList){
		if (newList == undefined && targetList == undefined)
		{
			var n_task = new Object();
			tasks.push(new_task);
			$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(tr.parents('table').find('tr:first'));
			tr.parents('table').find('tr:first').next().bind_task_row();
		}
		if (newList != undefined )
		{
			var ng = $('#tmpl_new_group_task').tmpl();
			$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(ng.find('table tr:first'));
			ng.insertAfter('div.list:first');
			$('div.list:first').next().find('table tr:first').next().bind_task_row();
			$('div.list:first').next().add_new_task_layout();			                                                        	
		}
		if (targetList != undefined)
		{
			$('#tmpl_new_task_in_group').tmpl(new_task).insertAfter(tr.parents('.tstyle_content')).find('.list-'+targetList).find('tr:first');
			tr.parents('.tstyle_content')).find('.list-'+targetList).find('tr:first').next().bind_task_row();
		}
	};
</script>